﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
{
    using System;
    using System.Data.Entity;
    using System.IO;
    using System.Reflection;

    // Summary:
    //     Sample Entity Framework 4.1 data initializer class.
    //     The seed method is executed after the database is created using EF 4.1 Code-First, 
    //     giving the developer the opportunity to insert "seed" (i.e. initial) data.
    //     For more information, visit the ADO.NET Entity Framework website at http://msdn.microsoft.com/data/aa937723
    public class SqlSampleDataInitializer : IDatabaseInitializer<SqlDataContext>
    {
        public void InitializeDatabase(SqlDataContext context)
        {
            var initializer = new InnerSqlSampleDataInitializer();

            // Initialize database using EF 4.1 Code-First.
            initializer.InitializeDatabase(context);

            // Set the command timeout to 90 seconds.
            ((System.Data.Entity.Infrastructure.IObjectContextAdapter)context).ObjectContext.CommandTimeout = 90;

            // Create common database objects required by the ASP.NET Providers for SQL Server.
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Samples.WindowsPhoneCloud.Web.Resources.InstallCommon.sql"))
            {
                var installCommonSqlCommands = new StreamReader(stream).ReadToEnd().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var sqlCommand in installCommonSqlCommands)
                {
                    if (!string.IsNullOrWhiteSpace(sqlCommand))
                    {
                        context.Database.ExecuteSqlCommand(sqlCommand);
                    }
                }
            }

            // Create database objects specifically required by the ASP.NET Membership Provider for SQL Server.
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Samples.WindowsPhoneCloud.Web.Resources.InstallMembership.sql"))
            {
                var installMembershipSqlCommands = new StreamReader(stream).ReadToEnd().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var sqlCommand in installMembershipSqlCommands)
                {
                    if (!string.IsNullOrWhiteSpace(sqlCommand))
                    {
                        context.Database.ExecuteSqlCommand(sqlCommand);
                    }
                }
            }
        }

        private class InnerSqlSampleDataInitializer : DropCreateDatabaseIfModelChanges<SqlDataContext>
        {
            protected override void Seed(SqlDataContext context)
            {
                context.AddSampleData(1, "I am the first title", true, DateTime.UtcNow);
                context.AddSampleData(2, "I am the second title", true, DateTime.UtcNow);
            }
        }
    }
}